/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistencia.controller.jpa;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import persistencia.controller.jpa.exceptions.NonexistentEntityException;
import persistencia.entidade.PortalCfg;

/**
 *
 * @author victorino
 */
public class PortalCfgJpaController  implements Serializable{

    public PortalCfgJpaController() {
        
    }
    
    public EntityManager getEntityManager() {
        return JdbcController.getInstance().getEntityManager();
    }

    public void create(PortalCfg portalCfg) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(portalCfg);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public PortalCfg getActivePortal(){
        // TODO: IMPLEMENTA FILTRANDO PELA SCHEM NAME
        List<PortalCfg> portal = findPortalCfgEntities();
        return portal.get(0);
    }

    public void edit(PortalCfg portalCfg) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            portalCfg = em.merge(portalCfg);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = portalCfg.getPortalId();
                if (findPortalCfg(id) == null) {
                    throw new NonexistentEntityException("The portalCfg with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PortalCfg portalCfg;
            try {
                portalCfg = em.getReference(PortalCfg.class, id);
                portalCfg.getPortalId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The portalCfg with id " + id + " no longer exists.", enfe);
            }
            em.remove(portalCfg);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<PortalCfg> findPortalCfgEntities() {
        return findPortalCfgEntities(true, -1, -1);
    }

    public List<PortalCfg> findPortalCfgEntities(int maxResults, int firstResult) {
        return findPortalCfgEntities(false, maxResults, firstResult);
    }

    private List<PortalCfg> findPortalCfgEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from PortalCfg as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public PortalCfg findPortalCfg(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(PortalCfg.class, id);
        } finally {
            em.close();
        }
    }

    public int getPortalCfgCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from PortalCfg as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
